package com.label.Web;

import com.label.Config.Logger.LogAnnotation;
import com.label.Model.Common.ResponseBean;
import com.label.Model.Common.Validation.Get;
import com.label.Model.Common.Validation.Save;
import com.label.Model.Common.Validation.Update;
import com.label.Model.Query.ActivityQuery;
import com.label.Service.ActivityService;
import com.label.Utils.UserUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.eclipse.jetty.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/activity")
public class ActivityController {

    private final static Logger logger = LoggerFactory.getLogger(ActivityController.class);

    @Resource
    private ActivityService activityService;

    @RequestMapping("/getOne")
    @LogAnnotation(module = "活动模块",operator = "通过ID获取活动")
    @RequiresAuthentication
    public ResponseBean getOne(@Validated(value = Get.class) @RequestBody ActivityQuery activityQuery){
        try {
            return new ResponseBean(HttpStatus.OK_200,"查找成功！",activityService.getOneById(activityQuery));
        }catch (Exception e){
            logger.info(e.getMessage());
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR_500,"查找失败！",null);
        }
    }

    @RequestMapping("/getAll")
    @LogAnnotation(module = "活动模块",operator = "获取所有活动")
    @RequiresAuthentication
    public ResponseBean getAll(){
        try {
            return new ResponseBean(HttpStatus.OK_200,"查找成功！",activityService.getAllActivity());
        }catch (Exception e){
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR_500,"查找失败！",null);
        }
    }

    @RequestMapping("/getAllByLevel")
    @LogAnnotation(module = "活动模块",operator = "根据等级获取活动")
    @RequiresAuthentication
    public ResponseBean getAllByLevel(@RequestBody ActivityQuery activityQuery){
        try {
            return new ResponseBean(HttpStatus.OK_200,"查找成功！",activityService.getAllByLevel(activityQuery));
        }catch (Exception e){
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR_500,"查找失败！",null);
        }
    }

    @RequestMapping("/getAllByName")
    @LogAnnotation(module = "活动模块",operator = "根据名称获取活动")
    @RequiresAuthentication
    public ResponseBean getAllByName(@RequestBody ActivityQuery activityQuery){
        try {
            return new ResponseBean(HttpStatus.OK_200,"查找成功！",activityService.getAllByName(activityQuery));
        }catch (Exception e){
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR_500,"查找失败！",null);
        }
    }

    @RequestMapping("/getActByUser")
    @LogAnnotation(module = "活动模块",operator = "获取用户参加的活动")
    @RequiresAuthentication
    public ResponseBean getActByUser(@Validated(value = Get.class) @RequestBody ActivityQuery activityQuery){
        return new ResponseBean(HttpStatus.OK_200,"查找成功！",activityService.getAllByUser(activityQuery.getId()));
    }

    @RequestMapping("/getMyAct")
    @LogAnnotation(module = "活动模块",operator = "获取当前用户参加的活动")
    @RequiresAuthentication
    public ResponseBean getMyAct(){
        return new ResponseBean(HttpStatus.OK_200,"查找成功！",activityService.getAllByUser(String.valueOf(UserUtil.getUserId())));
    }

    @RequestMapping("/delAct")
    @LogAnnotation(module = "活动模块",operator = "根据ID删除活动")
    @RequiresAuthentication
    public ResponseBean delAct(@RequestBody ActivityQuery activityQuery){
        return new ResponseBean(HttpStatus.OK_200,"成功！",activityService.delActivity(activityQuery));
    }

    @RequestMapping("/addAct")
    @LogAnnotation(module = "活动模块",operator = "添加活动")
    @RequiresAuthentication
    public ResponseBean addAct(@Validated(value = Save.class) @RequestBody ActivityQuery activityQuery){
        return new ResponseBean(HttpStatus.OK_200,"成功！",activityService.addActivity(activityQuery));
    }

    @RequestMapping("/updateAct")
    @LogAnnotation(module = "活动模块",operator = "更新活动信息")
    @RequiresAuthentication
    public ResponseBean updateAct(@Validated(value = Update.class) @RequestBody ActivityQuery activityQuery){
        try {
            return new ResponseBean(HttpStatus.OK_200,"成功！",activityService.updateActivity(activityQuery));
        }catch (Exception e){
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR_500,"更新失败！",null);
        }
    }

    @RequestMapping("/getActScoreByUser")
    @LogAnnotation(module = "活动模块",operator = "获取当前用户的得分信息")
    @RequiresAuthentication
    public ResponseBean getActScoreByUser(){
        try {
            return new ResponseBean(HttpStatus.OK_200,"成功！",activityService.getActScoreByUser());
        }catch (Exception e){
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR_500,"查找失败！",null);
        }
    }
}
